package com.lhys.controller;


import com.github.pagehelper.PageInfo;
import com.lhys.common.ApiResultMsg;
import com.lhys.common.ResultInfo;
import com.lhys.controller.base.BaseController;
import com.lhys.pojo.AssetStorage;
import com.lhys.service.AssetStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 资产_存放处表 前端控制器
 * </p>
 *
 * @author 小七
 * @since 2021-05-06
 */

@Api(tags = "存放地点模块")
@RestController
@RequestMapping("/api/asset-storage")
public class AssetStorageController extends BaseController {
    @Autowired
    private AssetStorageService assetStorageService;

    @PutMapping("/save")
    @ApiOperation(value = "存放地点模块添加或更新", authorizations = {@Authorization(value = header)})
    public ApiResultMsg saveAssetStorage(@RequestBody AssetStorage assetStorage) {
        Boolean aBoolean = assetStorageService.saveAssetStorage(assetStorage);
        if(aBoolean == null){
            return ApiResultMsg.error("该存放地点标题或编码已存在");
        }
        if (!aBoolean) {
            return ApiResultMsg.error(ResultInfo.ERROR);
        }
        return ApiResultMsg.success(ResultInfo.SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "存放地点模块删除", authorizations = {@Authorization(value = header)})
    public ApiResultMsg deleteAssetStorage(@PathVariable("id") Integer id) {
        Boolean aBoolean = assetStorageService.delAssetStorageById(id);
        if (!aBoolean) {
            return ApiResultMsg.error(ResultInfo.ERROR);
        }
        return ApiResultMsg.success(ResultInfo.SUCCESS);
    }

    @GetMapping("/getAssetStorage/{id}")
    @ApiOperation(value = "根据id查询存放地点", authorizations = {@Authorization(value = header)})
    public ApiResultMsg getAssetStorageById(@PathVariable("id") Integer id) {
        AssetStorage AssetStorage = assetStorageService.getAssetStorageById(id);
        return ApiResultMsg.success(ResultInfo.SUCCESS, AssetStorage);
    }

    @GetMapping("/getAssetStorage/{code}")
    @ApiOperation(value = "根据code查询存放地点", authorizations = {@Authorization(value = header)})
    public ApiResultMsg getAssetStorageByCode(@PathVariable("code") String code) {
        AssetStorage AssetStorage = assetStorageService.getAssetStorageByCode(code);
        return ApiResultMsg.success(ResultInfo.SUCCESS, AssetStorage);
    }

    @GetMapping("/getAssetStorage/{title}")
    @ApiOperation(value = "根据title查询存放地点", authorizations = {@Authorization(value = header)})
    public ApiResultMsg getAssetStorageByTitle(@PathVariable("title") String title) {
        AssetStorage AssetStorage = assetStorageService.getAssetStorageByTitle(title);
        return ApiResultMsg.success(ResultInfo.SUCCESS, AssetStorage);
    }

    @GetMapping("/list")
    @ApiOperation(value = "存放地点模块查询分页", authorizations = {@Authorization(value = header)})
    public ApiResultMsg listAssetStorageByPage(AssetStorage AssetStorage, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<AssetStorage> AssetStorageList = assetStorageService.getAssetStorageList(AssetStorage, pageNum, pageSize);
        return ApiResultMsg.success(ResultInfo.SUCCESS, AssetStorageList);
    }
}

